/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  zzz.test.component.direct.Test03
 * 
 *   Date:   2014-3-7
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.sample.component.direct;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

import com.titan.demo.camel.utils.CamelUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class Test03 {

    public static void main(String[] args) {
        CamelContext context = null;
        ProducerTemplate template = null;
        ConsumerTemplate consumer = null;
        try {
            context = new DefaultCamelContext();
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:in").process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            String body = exchange.getIn().getBody(String.class);
                            System.out.println("[ in]>>"+body);
                            exchange.getOut().setBody(body + " World");
                        }
                    });
                }
            });
            
            template = context.createProducerTemplate();
            template.start();
            consumer = context.createConsumerTemplate();
            consumer.start();
            context.start();
            
            for (int i = 0; i < 5; i++) {
                long time = System.currentTimeMillis();
                template.sendBody("direct:in", "Hello["+i+"]");
                time = System.currentTimeMillis() - time;
                System.out.println("\t["+i+"] 用时：["+time+"]。");
            }
            
            Object response = template.requestBody("direct:in", "Hello");
            System.out.println("[out]>>"+response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CamelUtils.close(consumer);
            CamelUtils.close(template);
            CamelUtils.close(context);
        }
    }

}

